const multer = require('multer');
const path = require('path');

// 设置文件存储
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'uploads/') // 设置存储文件的目录
  },
  filename: function (req, file, cb) {
    cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname))  // 设置文件名
  }
});

// 文件类型过滤
const fileFilter = (req, file, cb) => {
  // 将允许的文件类型存储在一个数组中
  const allowedTypes = ['image/jpeg', 'image/png', 'image/jpg', 'audio/mpeg'];

  if (allowedTypes.includes(file.mimetype)) {
    cb(null, true);
  } else {
    cb(null, false);
  }
};

const upload = multer({ 
  storage: storage,
  fileFilter: fileFilter
});

module.exports = upload;
